package com.ruoyi.project.emr.controller;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.project.emr.domain.EmrDrug;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.emr.domain.EmrPrescription;
import com.ruoyi.project.emr.service.IEmrPrescriptionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 处方Controller
 *
 * @author gzu
 * @date 2023-07-22
 */
@RestController
@RequestMapping("/emr/prescription")
public class EmrPrescriptionController extends BaseController
{
    @Autowired
    private IEmrPrescriptionService emrPrescriptionService;

    /**
     * 获得处方列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmrPrescription emrPrescription)
    {
        startPage();
        List<EmrPrescription> list = emrPrescriptionService.selectEmrPrescriptionList(emrPrescription);
        return getDataTable(list);
    }

    /**
     * 获取处方详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emrPrescriptionService.selectEmrPrescriptionById(id));
    }

    /**
     * 新增处方
     */
    @PostMapping
    public AjaxResult add(@RequestBody EmrPrescription emrPrescription)
    {
        if (emrPrescriptionService.insertEmrPrescription(emrPrescription) > 0) {
            return success();
        } else {
            return error("server internal error");
        }
    }

    /**
     * 修改处方
     */
    @PutMapping
    public AjaxResult edit(@RequestBody EmrPrescription emrPrescription)
    {
        if (emrPrescriptionService.updateEmrPrescription(emrPrescription) > 0) {
            return success();
        } else {
            return error("server internal error");
        }
    }

    /**
     * 删除处方
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        emrPrescriptionService.deleteEmrPrescriptionByIds(ids);
        return success();
    }

    /**
     * 获取处方所含药物
     * @param params 内含处方id
     * @return 返回处方所含药品列表
     */
    @PostMapping(value = "/drugs")
    public AjaxResult listDrugsOfPrescription(@RequestBody Map<String, Integer> params) {

        Integer preId = params.get("preId");
        List<EmrDrug> drugs = emrPrescriptionService.selectDrugsOfPrescription(preId);

        return success(drugs);
    }
}
